<?php

sleep(rand(1,10));


echo "<pre>";
/**
 * 
 * 根据用户的粉丝列表抓取可能的粉丝
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}




define("CRON_NAME","cron_sina_blog_tuiguang");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/gonglue/inc.php");

ini_set("display_errors",ture);
error_reporting(E_ALL);


class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
	
		
		$this->after_run();
	}
	function get_data()
	{
		
		$db_gonglue = new db_gonglue();
		$this->start_id =( $this->start_id) + 5;
		
		$get_list = $db_gonglue->cron_get_gonglue_topic_list(1,100,$this->start_id);
		
		
		foreach($get_list as $key => $value)
		{
			$content = $value["content"];
			$content_short = preg_replace("/\<.*?\>/","",$content);
			if(strlen($content_short) > 400)
			{
				$res = $this->do_add_mblog($value["title"],$content,$value["id"],$value["topic_id"] );
				
				if($value["id"])$this->replace_into_data($value["id"]);	
				
				break;
			}
		}
	}	
	
	
	function do_add_mblog($title,$content,$id,$topic_id)
	{
		$content = preg_replace("/\<a.*?\>/","",$content);var_dump($content);
		$content = preg_replace("/\<\/a.*?\>/","",$content);var_dump($content);
		$content  = str_replace("/data/urlget/","http://www.yxxiu.com//data/urlget/",$content);
		
		
		
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($topic_id);
		$topic = $topic_info["topic"] ? $topic_info["topic"] : "游戏";
		$content = "本文来自-> <a href=\"http://www.yxxiu.com/gonglue/detail.php?id={$id}\" target=\"_blank\">{$title}</a><br><br> ".$content;
		
		$title = "[转载]{$topic}资讯攻略-".$title;
		
		
		$param = array();
		$param["articleStatus_preview"] = 1;
		$param["assoc_style"] = 1;
		$param["blog_body"] = $content;
		$param["tag"] = "游戏,{$topic}";
		
		$param["blog_class"] = "00";
		$param["blog_title"] = $title;
		$param["conlen"] = 4;
		$param["date_pub"] = date("Y-m-d");
		$param["immediatepub"] = 0;
		$param["isTimed"] = 0 ;
		$param["is_album"] = 0;
		$param["is_media"] = 0;
	
		$param["is_stock"] = 0;
		$param["is_tpl"] = 0;
		$param["join_circle"] = 1;
		$param["old365"] = 0;
		$param["sina_sort_id"] = 118;
		$param["time"] = date("H:i:s");
		$param["topic_channel"] = 0;
	

		$param["topic_id"] = 0;
		$param["utf8"] = 1;
		$param["vtoken"] = "7b1892b8639d7a9f1963af3a5ffe5992";
		$param["x_cms_flag"] = 0;
	
	
		var_dump($param);
		$url="http://control.blog.sina.com.cn/admin/article/article_post.php";
		$cookie="EditorToolType=base; _s_loginuid=2632497997; _s_loginStatus=2632497997; blogPopularizeIndex=2; msnBindInfo=2632497997_0; U_TRS1=000000c3.86ed6a5.51021f6b.56b5571d; vjuids=36700329d.13c704ab373.0.a28fb898813cf8; vjlast=1359131887.1359216115.11; UOR=control.blog,blog,; SINAGLOBAL=0ad511ae.e8ab0.51021f8d.efb11744; ULV=1359289813775:9:9:2:00000072.621815e7.51051ddf.2f20b675:1359216112480; FSINAGLOBAL=000000c3.938d4e52.50c03f45.306173d4; 2632497997alreadyVisitProfileIndex=true; lxlrttp=1359068396; U_TRS2=00000072.5f5647ca.51051ddc.9565c09a; SessionID=gbk6kdng2mephkmuimgh213hn2; BLOG_TITLE=%E6%B8%B8%E6%88%8F%E7%A7%80%E7%9A%84%E5%8D%9A%E5%AE%A2; _s_upa=8; Apache=00000072.621815e7.51051ddf.2f20b675; _u_poll=blog%2C1%2C50; rotatecount=1; PHPSESSID=j7cq11brhlu74dkq03kolskeq7; SUS=SID-2632497997-1359290373-XD-gawdz-f969339d64bd63ee8b5e26638df88754; SUE=es%3D2ca2a72605e1aeba74a1576f1904049d%26ev%3Dv1%26es2%3D4b994e073bba25ff53dd8bd9339ed86f%26rs0%3Dky27U%252BJ9J3ppXYPLFofZ9Y%252BVhz%252F6oJpH1WX9xbPGsVQtxxZWCEu3xxseXnEBvJurlDMwpu3H477QxfFRXCK14%252BnmDn3xT8%252BNKUIgr5a8IcquzSwznfBvAPM2JVUEWFCxMGRtCjlg7Xk4dzukSreG8OyZRZg0fFVsvcgs398XVtY%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1359290373%26et%3D1359376773%26d%3D40c3%26i%3D8754%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D0%26lt%3D1%26uid%3D2632497997%26user%3Dyangchaohi110.%252A%252A%26ag%3D9%26name%3Dyangchaohi110%2540sina.com%26nick%3D%25E6%25B8%25B8%25E6%2588%258F%25E7%25A7%2580%26sex%3D1%26ps%3D0%26email%3Dyangchaohi110%2540sina.com%26dob%3D1986-02-01%26ln%3Dyangchaohi110%2540sina.com%26os%3D%26fmp%3D%26lcp%3D; ALF=1361882373; SUR=uid%3D2632497997%26user%3Dyangchaohi110.%252A%252A%26nick%3D%25E6%25B8%25B8%25E6%2588%258F%25E7%25A7%2580%26email%3Dyangchaohi110%2540sina.com%26dob%3D1986-02-01%26ag%3D9%26sex%3D1%26ssl%3D1; SINABLOGNUINFO=2632497997.9ce8bb4d.xiuyouxi";
		$res = $this->mblog_curl_request("post",$url,$cookie,$param);
		
		
		$res = json_decode($res,true);
		
		var_dump($res);
		
		if($res["code"]=="B06001" || $res["data"])
		{
			return true;
		}
		else
		{
			return false;
		}
		exit;
		
		
	}
	function mblog_curl_request($type="get",$url,$cookie_list="",$params_arr=array(),$auth_user="",$auth_passwd="",$muliti=false)
	{
		
		if($type == "get")
		{
			$ch = curl_init();
			
			if (strlen($auth_user) > 0)
	        {
	        	curl_setopt($ch, CURLOPT_USERPWD, "{$auth_user}:{$auth_passwd}");  
	        }   
			
	        
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER,true);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			if($cookie_list)
			{
				curl_setopt($ch, CURLOPT_COOKIE, $cookie_list);
			}
	
		}
		elseif($type == "post")
		{
			
			
			
			$fields_string = "";
			if($muliti)
			{
				$fields_string = $this->build_http_query_multi($params_arr);
				$header_array2 = array("Content-Type: multipart/form-data; boundary=" . $this->boundary );
	        }
			else 
			{
				if (is_array($params_arr) && $params_arr)
		        {
			        foreach($params_arr as $key=>$value) 
			        { 
			            $fields_string .= $key.'='.urlencode($value).'&' ; 
			        }
			        $fields_string = rtrim($fields_string ,'&');
		        }
		        else 
		        {
		        	 $fields_string .= $params_arr; //urlencode($params_arr);
		        }
				
				}
           
			
			$ch = curl_init();
			
			if (strlen($auth_user) > 0)
	        {
	        	curl_setopt($ch, CURLOPT_USERPWD, "{$auth_user}:{$auth_passwd}");  
	        }   
	        
	        if($muliti)
	        {
	        	curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array2 ); 
	        }
			curl_setopt($ch, CURLOPT_URL, $url);
			
			
			
			curl_setopt($ch, CURLOPT_REFERER,"http://control.blog.sina.com.cn/admin/article/article_add.php");
			curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0");
			
			
			curl_setopt($ch, CURLOPT_HEADER,true);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_POST,count($params_arr));
			curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
			
			if($cookie_list)
			{
				curl_setopt($ch, CURLOPT_COOKIE, $cookie_list);
			}
			 
		}
		
		//$time_start = microtime_float();
		$res = curl_exec($ch);
		
		$res2 = curl_getinfo($ch);
		curl_close($ch);		
		//$time_using = microtime_float() - $time_start;
		//global $program_wapi_call_time;
		//$program_wapi_call_time = $program_wapi_call_time + $time_using;
		// 性能监控
		//$this->perf_add_count($url);                              //加入性能分析
	
		
		$res = explode("\r\n\r\n",$res);
		$content_json = array_pop($res);
		$header = array_pop($res);
		
		$content = json_decode($content_json, TRUE);
		
		
		
		$params_arr["cookie_list"] = $cookie_list;
		$params_arr["basic_info"] = $auth_user;
		
		/*if(substr($header,0,15) != "HTTP/1.1 200 OK")
		{
			$error_code = substr($header,9,3);
			$error_code = intval($error_code) * 1000;
			$this->add_api_tongji_v2($url,$error_code,$content['error'],$header,serialize($params_arr),$time_using,$content_json);
		}
		else
		{
			//$this->add_api_tongji_v2($url,$content['error_code'],$content['error'],$header,serialize($params_arr),$time_using,$content_json);
		}*/
		
		
		if(isset($_GET["mblog_debug"]))
		{
			echo "<pre>";
			echo "request_url:".$url."\n";
			echo "type: $type<br>\n\r";
			echo "auth_user: $auth_user<br>\n\r";
			echo "auth_passwd: $auth_passwd<br>\n\r";
			echo "cookie_list: $cookie_list<br>\n\r";
			echo "request_result:";var_dump($res);
			echo "params:";
			var_dump($params_arr);
			echo "fields_string:";
			var_dump($fields_string);
		}
		
		return $content_json;

	}
	
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
